package top.api.runner;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import top.api.Constants.SystemConstants;
import top.api.domain.entity.Article;
import top.api.mapper.ArticleMapper;
import top.api.utils.RedisCache;

import java.util.List;

/**
 * 将文章浏览量全部缓冲到 redis
 */
@Component
@Slf4j
public class ViewCountRunner implements CommandLineRunner {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public void run(String... args) throws Exception {
        log.info("ViewCountRunner开始");
        List<Article> articles = articleMapper.selectList(null);
        for (Article article : articles) {
            redisCache.setCacheMapValue(SystemConstants.REDIS_VIEWCOUNT_KEY,article.getId().toString(),article.getViewCount().intValue());
        }
        log.info("ViewCountRunner结束");
    }
}
